# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from pollango.account.models import PhoneType
from pollango.account.bforms import PhoneTypeForm
import datetime
from django.shortcuts import render_to_response

def render(template, payload):
    payload['list'] = PhoneType.all().order('-created_on').fetch(5)
    return render_to_response(template, payload)

def display(request):
    if request.method == 'POST':
        form = PhoneTypeForm(request.POST)
        if(form.is_valid()):
            form.save()
    payload = dict(form=PhoneTypeForm())
    return render('account/phonetypes.html',payload)


def delete(request,phonetype_key):
    phoneType = PhoneType.get(phonetype_key)
    phoneType.delete()
    return HttpResponseRedirect('/account/')

def edit(request,phonetype_key):
    if request.method == 'POST':
        phoneType = PhoneType.get(phonetype_key)
        form = PhoneTypeForm(request.POST,instance = phoneType)
        form.save()
        return HttpResponseRedirect('/account/')
    else:
        phoneType = PhoneType.get(phonetype_key)
        form = PhoneTypeForm(instance = phoneType)
        payload = dict(form=form)
        return render('account/phonetypes.html',payload)
    
